﻿using SCG.General;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TTEngine
{
    public class Door : MapObject
    {
        //constructor
        public Door()
            : base()
        {
            IsOpen = false;
            Flamability = 5;
            BurnLifetime = Globals.RandomIntFromTo(10, 20);
        }

        //clone
        public override MapObject Clone()
        {
            return new Door();
        }

        //second coordinate
        public int X2
        {
            get
            {
                return Orientation == SCGOrientation.Horizontal ? X + 1 : X;
            }
        }
        public int Y2
        {
            get
            {
                return Orientation == SCGOrientation.Horizontal ? Y : Y + 1;
            }
        }

        //public properties
        public SCGOrientation Orientation { get; set; }

        //is it open?
        public bool IsOpen
        {
            get
            {
                return _isOpen;
            }
            set
            {
                _isOpen = value;
                BlocksMovement = !IsOpen;
                if (Level != null)
                {
                    if (IsOpen)
                    {
                        _autoClose = Globals.RandomIntFromTo(5, 20);
                        Level.TerrainMap.Set(X, Y, Globals.TERRAIN_FLOOR);
                        Level.TerrainMap.Set(X2, Y2, Globals.TERRAIN_FLOOR);
                    }
                    else
                    {
                        Level.TerrainMap.Set(X, Y, Globals.TERRAIN_DOOR);
                        Level.TerrainMap.Set(X2, Y2, Globals.TERRAIN_DOOR);
                    }
                }
            }
        }

        //added to map - alter terrain
        public override void AddedToMap()
        {
            Level.TerrainMap.Set(X, Y, Globals.TERRAIN_DOOR);
            Level.TerrainMap.Set(X2, Y2, Globals.TERRAIN_DOOR);
        }

        //actions
        public override List<string> GetActionsAdjacent(Actor actor)
        {
            List<string> lst = base.GetActionsAdjacent(actor);
            if (IsOpen)
            {
                lst.Add("Close");
            }
            else
            {
                lst.Add("Open");
            }
            return lst;
        }
        public override void ProcessAdjacentAction(Actor actor, string action, int qty)
        {
            switch (action)
            {
                case "Open":
                    {
                        IsOpen = true;
                        break;
                    }
                case "Close":
                    {
                        IsOpen = false;
                        break;
                    }
                default:
                    {
                        base.ProcessAdjacentAction(actor, action, qty);
                        break;
                    }
            }
        }

        //auto-close
        public override void ProcessImpulse()
        {
            base.ProcessImpulse();
            if (IsOpen)
            {
                if (!Level.IsEmpty(X, Y))
                    return;
                if (!Level.IsEmpty(X2, Y2))
                    return;
                if (_autoClose-- <= 0)
                    IsOpen = false;
            }
        }

        //burning should reset terrain
        public override void Burn()
        {
            base.Burn();
            Level.TerrainMap.Set(X, Y, Globals.TERRAIN_FLOOR);
            Level.TerrainMap.Set(X2, Y2, Globals.TERRAIN_FLOOR);
            Fire f2 = new Fire();
            f2.Lifetime = BurnLifetime;
            Level.AddObjectToMap(f2, X2, Y2);
        }

        //private members
        private bool _isOpen = false;
        private int _autoClose;
    }
}
